$(function(){
    // 显示登录名称
    $(".shortcut .w .fr ul li:eq(0) a:eq(0)").text(localStorage.getItem("uName"));
    $(".jd_login_top .text a:eq(0)").text(localStorage.getItem("uName"));

    // 主题内容部分左侧广告栏
    $(".main_guanggao_box").hover(function(){
    $(".main_l_guanggao_con").stop().animate({
        width:'toggle'
    },250)
    })
    // 主题内容部分左侧菜单栏
    $(".main_left ul li").mouseover(function(){
        $(this).find(".main_left_cd").show();
    })
    $(".main_left ul li").mouseout(function(){
        $(this).find(".main_left_cd").hide();
    })
    // 主体内容部分左侧轮播
    var time1 = setInterval(function(){
        $(".focus_left .btn_right").click();
        },3000)
    $(".focus_left").mouseover(function(){
        clearInterval(time1);
        time=null;
    })
    $(".focus_left").mouseout(function(){
        time1 = setInterval(function(){
            $(".focus_left .btn_right").click();
            },3000)
    })
    var imgs = new Array("images/q1.jpg","images/q2.jpg","images/q3.jpg","images/q4.jpg","images/q5.jpg","images/q6.jpg","images/q7.jpg","images/q8.jpg");
    var i=0;
    $(".focus_left .btn_right").click(function(){
            if(i==imgs.length-1){
                $(".focus_left").css({"background":"url("+imgs[0]+")","background-size":"cover"});
                $(".yd li").eq(0).css("background","#fff").siblings().css("background","rgba(255,255,255,.4)");
                i=0;
            }else{
                i++;
                $(".focus_left").css({"background":"url("+imgs[i]+")","background-size":"cover"});
                $(".yd li").eq(i).css("background","#fff").siblings().css("background","rgba(255,255,255,.4)");
            }
    })
    $(".focus_left .btn_left").click(function(){
        if(i==0){
            $(".focus_left").css({"background":"url("+imgs[imgs.length-1]+")","background-size":"cover"});
            $(".yd li").eq(imgs.length-1).css("background","#fff").siblings().css("background","rgba(255,255,255,.4)");
            i=imgs.length-1;
        }else{
            i--;
            $(".focus_left").css({"background":"url("+imgs[i]+")","background-size":"cover"});
            $(".yd li").eq(i).css("background","#fff").siblings().css("background","rgba(255,255,255,.4)");
        }
    })
    $(".yd li").mouseover(function(){
        var index = $(this).index();
        $(".focus_left").css({"background":"url("+imgs[index]+")","background-size":"cover"});
        $(this).css("background","#fff").siblings().css("background","rgba(255,255,255,.4)");
        i=index;
    })
       // 主体内容部分右侧轮播
       var time2 = setInterval(function(){
        $(".focus_right .btn_right").click();
    },7000)
       $(".focus_right").mouseover(function(){
        $(".focus_right .btn_right").show();
        $(".focus_right .btn_left").show();
        clearInterval(time2);
        time2=null;
    })
    $(".focus_right").mouseout(function(){
     $(".focus_right .btn_right").hide();
     $(".focus_right .btn_left").hide();
     time2 = setInterval(function(){
        $(".focus_right .btn_right").click();
    },5000)
 })
       var main_img1 = new Array("images/main_r1.jpg","images/main_r1_2.jpg","images/main_r1_3.jpg");
       var main_img2 = new Array("images/main_r2.jpg","images/main_r2_2.jpg","images/main_r2_3.jpg");
       var main_img3 = new Array("images/main_r3.jpg","images/main_r3_2.jpg","images/main_r3_3.jpg");
       var j=0;
       $(".focus_right .btn_right").click(function(){
           if(j==main_img1.length-1){
            $(".focus_right ul li:eq(0)").css({"background":"url("+main_img1[0]+")","background-size":"cover"});
            $(".focus_right ul li:eq(1)").css({"background":"url("+main_img2[0]+")","background-size":"cover"});
            $(".focus_right ul li:eq(2)").css({"background":"url("+main_img3[0]+")","background-size":"cover"});
            j=0;
           }else{
               j++;
               $(".focus_right ul li:eq(0)").css({"background":"url("+main_img1[j]+")","background-size":"cover"});
               $(".focus_right ul li:eq(1)").css({"background":"url("+main_img2[j]+")","background-size":"cover"});
               $(".focus_right ul li:eq(2)").css({"background":"url("+main_img3[j]+")","background-size":"cover"});
           }
       })
       $(".focus_right .btn_left").click(function(){
        if(j==0){
         $(".focus_right ul li:eq(0)").css({"background":"url("+main_img1[main_img1.length-1]+")","background-size":"cover"});
         $(".focus_right ul li:eq(1)").css({"background":"url("+main_img2[main_img1.length-1]+")","background-size":"cover"});
         $(".focus_right ul li:eq(2)").css({"background":"url("+main_img3[main_img1.length-1]+")","background-size":"cover"});
         j=main_img1.length-1;
        }else{
            j--;
            $(".focus_right ul li:eq(0)").css({"background":"url("+main_img1[j]+")","background-size":"cover"});
            $(".focus_right ul li:eq(1)").css({"background":"url("+main_img2[j]+")","background-size":"cover"});
            $(".focus_right ul li:eq(2)").css({"background":"url("+main_img3[j]+")","background-size":"cover"});
        }
    })
    // 主题内容部分右侧新闻服务列表模块
    var main_fw_img = new Array("images/main_fw1.png","images/main_fw2.png","images/main_fw3.png","images/main_fw4.png","images/main_fw5.png","images/main_fw6.png","images/main_fw7.png","images/main_fw8.png","images/main_fw9.png","images/main_fw10.png","images/main_fw11.png","images/main_fw12.png",);
    var main_fw_img1 =new Array("images/eed6f6cbf1de3aaa.png","images/afb4399323fe3b76.png","images/5fee386254dd2ebc.png","images/caa83ce9b881cd24.png","images/f1f6dc5c207329f9.png","images/0004c1b85fbf7bde.png","images/1d0957d7f2ae01a2.png","images/d4d1151b09b5553b.png","images/e6976f3cb30c9a8a.png","images/dd4d9ef7ce8fc169.png","images/d6f3909618c6307a.png","images/97917a2daa34be0f.png");
    $(".service_list ul li").mouseover(function(){
        var index = $(this).index();
        $(".service_list ul li:eq("+index+") a img").attr("src",""+main_fw_img[index]+"");
        $(".service_list ul li:eq("+index+") a span").css("color","#c81623");
    })
    $(".service_list ul li").mouseout(function(){
        var index = $(this).index();
        $(".service_list ul li:eq("+index+") a img").attr("src",""+main_fw_img1[index]+"");
        $(".service_list ul li:eq("+index+") a span").css("color","#333");
    })
    /*
    京东秒杀模块倒计时效果
    var inputTime = +new Date("2022-12-4 20:00:00");
    
    countDown();
    setInterval(countDown,1000);
    function countDown() {
        var nowTime = +new Date();
        var times=(inputTime - nowTime) / 1000;
        var h=parseInt(times / 60 / 60 % 24);
        h = h<10? '0'+h:h;
        $(".time_hour").html(h);
        var m=parseInt(times / 60 % 60 );
        m = m<10? '0'+m:m;
        $(".time_minute").html(m);
        var s=parseInt(times % 60);
        s = s<10? '0'+s:s;
        $(".time_second").html(s);
    }
    */
    //1.想要控制谁就先获取谁 这里用到的是class 获取的方法
    const oHour = document.querySelector(".hour");
    const oMinute = document.querySelector(".minute");
    const oSecond = document.querySelector(".second");

    //2.储存秒杀的截至时间
    const remDate = new Date("2022-12-07 24:00:00") //秒杀截至的时间

    setTime(remDate);

    /*开启定时器 
    方法： setInterval( 函数体，时间间隔)
    */
    setInterval(
        function() {
            setTime(remDate);
        },1000);

    //为了方便测试 一进来 就有效果 而不是3个00
    //所以这里对其进行封装处理
    function setTime(remDate) {
        const obj = getDifferTime(remDate);
        //console.log("使用obj来储存时间的差值" + obj);

        //3.将差值设置给元素
        //类似于数组中的this.oHour = obj.hour
        oHour.innerText = obj.hour;
        oMinute.innerText = obj.minute;
        oSecond.innerText = obj.second;
    }

    function getDifferTime(remDate, curDate = new Date()) {
        //1.得到两数之间的是时间差值 （毫秒之间的）
        const differTime = remDate - curDate;
        //console.log("当前的毫秒数是" + differTime);

        //2.得到两数之间的时间差值 (秒)  1秒 = 1000毫秒

        const differSecond = differTime / 1000;

        //console.log("当前的时间差" + differSecond);

        /*3.利用相差的总秒数 换算成每一天的秒数 eg: 总秒数 / 每一天的秒数 = 相差的天数
        
        let day = Math.fllor(differSecond / (60 * 60 * 24));
        day = day >= 10 ? day : "0" + day;*/

        if (differSecond > 0) {
            //console.log("当前的秒数是" + differSecond);
        
        //4.利用相差的总秒数 / 小时 % 24
        //Math.floor 这个叫向下取整 也就是如果Math.floor(123.999) 他会打印出Math.floor(123);
        //与他相反的还有Math.ceil 。

        let hour = Math.floor(differSecond / (60 * 60) % 24);
        hour = hour >= 10 ? hour : "0" + hour;

        //5.利用相差的总秒数 / 分钟 % 60
        let minute = Math.floor(differSecond / 60 % 60);
        minute = minute >= 10 ? minute : "0" + minute;

        //6.利用相差的总秒数 % 秒数(60代表的意思是1分钟等于60秒)
        let second = Math.floor(differSecond % 60); //取整数
        second = second >= 10 ? second : "0" + second;

        //返回绑定完的天数  小时 分钟 秒
        return {
            //day : day,
            hour : hour,
            minute : minute,
            second : second,
        }
        } else {
            alert("请在index.js代码中的第147行更改秒杀截至时间，否者秒杀时间将显示为00：00：00");
        }
        
    }

    // 京东秒杀模块秒杀列表轮播
    var k=0;
    $(".seckill_list .btn_right").click(function(){
        if(k==-4000){
            $(".seckill_list ul").css("left","0px");
            k=0;
        }
        k=k-800;
            $(".seckill_list ul").stop().animate({
                left : k
            });
    })
    $(".seckill_list .btn_left").click(function(){
        if(k==0){
            $(".seckill_list ul").css("left","-4000px");
            k=-4000;
        }
            k=k+800;
        $(".seckill_list ul").stop().animate({
            left : k
        });
    })
    // 京东秒杀右侧固定侧边栏
    $(".guding_dh li a").mouseover(function(){
        $(this).css("background","#c81623");
        $(this).find(".kj_text span").css("color","#fff");
        $(this).find(".kj_text .kj_img").css("color","#fff");
        $(this).find("em").css("display","none");
    })
    $(".guding_dh li a").mouseout(function(){
        $(this).css("background","#fff");
        $(this).find(".kj_text span").css("color","#333");
        $(this).find(".kj_text .kj_img").css("color","#333");
        $(this).find("em").css("display","block");
    })

    var jd_seckill_top =  $(".jd_seckill").offset().top;
    $(window).scroll(function(){
        if($(document).scrollTop() >= jd_seckill_top-100){
           $(".guding_dh").css({"position":"fixed","top":"50px","right":"80px"});
           $(".guding_dh li:last").slideDown("fast");
        }else{
            $(".guding_dh").css({"position":"absolute","top":"751px","right":"80px"});
            $(".guding_dh li:last").slideUp("fast");
            // $(".guding_dh li:first a .kj_text span").css("color","#333");
        }

    })
    $(".guding_dh li:first a").click(function(){
        $("body,html").stop().animate({
            scrollTop : jd_seckill_top-100
        })
    }) 

    var jd_tese_top = $(".jd_tese").offset().top;
    $(".guding_dh li").eq(1).find("a").click(function(){
        $("body,html").stop().animate({
            scrollTop : jd_tese_top-20
        })
    })

    var jd_tuijian_top = $(".jd_tuijian").offset().top;
    $(".guding_dh li").eq(2).find("a").click(function(){
        $("body,html").stop().animate({
            scrollTop : jd_tuijian_top-20
        })
    })

    // 返回顶部
    $(".guding_dh li:last").click(function(){
       $("body,html").stop().animate({
           scrollTop : 0
       })
    })
    // 发现好货模块自动播放
    var goods_num = 0;
    $(".goods_list .btn_right").click(function(){
        if(goods_num==-1900){
            $(".goods_list ul").css("left","0px");
            goods_num = 0;
        }
        goods_num=goods_num-950;
        $(".goods_list ul").stop().animate({
            left: goods_num
        },10000)
    })
    $(".goods_list .btn_right").click();//页面加载自动播放
    var time3 = setInterval(function(){
        $(".goods_list .btn_right").click();
    },10000)

    // 特色优选模块tab栏样式切换
    $(".tab_head ul li:first").children("a").css({"borderBottom":"3px solid #e1251b","color":"#e1251b"});
    $(".tab_head ul li:first").find(".tob_body").show();
    $(".tab_head ul li:first").siblings(".tab_head ul li").find(".tob_body").hide();
    $(".tab_head ul li").mouseover(function(){
        $(this).children("a").css({"borderBottom":"3px solid #e1251b","color":"#e1251b"});
        $(this).siblings("li").children("a").css({"border":"0px","color":"#999"});
        $(this).find(".tob_body").show();
        $(this).siblings(".tab_head ul li").find(".tob_body").hide();
    })
    // 为你推荐模块tab栏切换
    $(".tab_title .tab_title_name").click(function(){
        $(this).find(".tab_class").css({"background":"#e1251b","border-radius":"50px","color":"#fff"});
        $(this).siblings(".tab_title_name").find(".tab_class").css({"background":"transparent","border-radius":"0","color":"#333"});
        $(this).find(".tab_text").css({"color":"#e1251b"});
        $(this).siblings(".tab_title_name").find(".tab_text").css({"color":"#999"});
    })
    $(".tab_title").children(".tab_title_name").click(function(){
        var index = $(this).index();
        $(".tuijian_tab_con .tuijian_tab_list").eq(index).show();
        $(".tuijian_tab_con .tuijian_tab_list").eq(index).siblings(".tuijian_tab_con .tuijian_tab_list").hide()
    })

})
